﻿using Gargoyle.Roton;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Gargoyle.ZZT
{
	class ZZTDisplay : DisplayCommon
	{
        public ZZTDisplay(Game g, Video input)
            : base(g, input)
        {
            fieldRect = new Rectangle(1, 1, 60, 25);
            fieldTargetLocation = new Point(0, 0);
        }

        public override void DrawMessage(ByteString message, int color)
        {
        }

        public override void EraseMessage()
        {
        }

        public override void FadeIn()
        {
        }

        public override void FadeOut()
        {
        }

        public override void LongTextInput()
        {
        }

        public override void Range(ByteString text)
        {
        }

        public override void RangeBinary(ByteString text)
        {
        }

        public override void RangeDirection(ByteString text)
        {
        }

        public override void RedrawBoard()
        {
        }

        private void RedrawHud()
        {
            for (int y = 0; y < 25; y++)
                RedrawHudLine(y);
        }

        private void RedrawHudLine(int y)
        {
            outputVideo.WriteString(new Point(60, y), "                    ", 0x1F);
        }

        public override void RedrawHudEditor()
        {
        }

        public override void RedrawHudGame()
        {
            outputVideo.WriteString(new Point(65, 0), ("- - - - -"), 0x1F);
            outputVideo.WriteString(new Point(62, 1), ("      ZZT      "), 0x70);
            outputVideo.WriteString(new Point(65, 2), ("- - - - -"), 0x1F);
            outputVideo.SetChar(new Point(62, 7), new AnsiChar(0x02, 0x1F));
            outputVideo.WriteString(new Point(65, 7), ("Health:"), 0x1E);
            outputVideo.SetChar(new Point(62, 8), new AnsiChar(0x84, 0x1B));
            outputVideo.WriteString(new Point(67, 8), ("Ammo:"), 0x1E);
            outputVideo.SetChar(new Point(62, 9), new AnsiChar(0x9D, 0x16));
            outputVideo.WriteString(new Point(64, 9), ("Torches:"), 0x1E);
            outputVideo.SetChar(new Point(62, 10), new AnsiChar(0x04, 0x1B));
            outputVideo.WriteString(new Point(67, 10), ("Gems:"), 0x1E);
            outputVideo.WriteString(new Point(66, 11), ("Score:"), 0x1E);
            outputVideo.SetChar(new Point(62, 12), new AnsiChar(0x0C, 0x1F));
            outputVideo.WriteString(new Point(67, 12), ("Keys:"), 0x1E);
            outputVideo.WriteString(new Point(62, 14), (" T "), 0x70);
            outputVideo.WriteString(new Point(66, 14), ("Torch"), 0x1F);
            outputVideo.WriteString(new Point(62, 15), (" B "), 0x30);
            outputVideo.WriteString(new Point(66, 15), ("Be "), 0x1F);
            outputVideo.WriteString(new Point(62, 16), (" H "), 0x70);
            outputVideo.WriteString(new Point(66, 16), ("Help"), 0x1F);
            outputVideo.WriteString(new Point(67, 18), new byte[] { 0x20, 0x18, 0x19, 0x1A, 0x1B }, 0x30);
            outputVideo.WriteString(new Point(73, 18), ("Move"), 0x1F);
            outputVideo.WriteString(new Point(61, 19), (" Shift"), 0x70);
            outputVideo.WriteString(new Point(67, 19), new byte[] { 0x20, 0x18, 0x19, 0x1A, 0x1B }, 0x70);
            outputVideo.WriteString(new Point(73, 19), ("Shoot"), 0x1F);
            outputVideo.WriteString(new Point(62, 21), (" S "), 0x70);
            outputVideo.WriteString(new Point(66, 21), ("Save game"), 0x1F);
            outputVideo.WriteString(new Point(62, 22), (" P "), 0x30);
            outputVideo.WriteString(new Point(66, 22), ("Pause"), 0x1F);
            outputVideo.WriteString(new Point(62, 23), (" Q "), 0x70);
            outputVideo.WriteString(new Point(66, 23), ("Quit"), 0x1F);
        }

        public override void RedrawHudTitle()
        {
            outputVideo.WriteString(new Point(65, 0), ("- - - - -"), 0x1F);
            outputVideo.WriteString(new Point(62, 1), ("      ZZT      "), 0x70);
            outputVideo.WriteString(new Point(65, 2), ("- - - - -"), 0x1F);
            outputVideo.WriteString(new Point(62, 5), ("Pick a command:"), 0x1B);
            outputVideo.WriteString(new Point(62, 7), (" W "), 0x30);
            outputVideo.WriteString(new Point(66, 7), ("World:"), 0x1E);
            outputVideo.WriteString(new Point(62, 11), (" P "), 0x70);
            outputVideo.WriteString(new Point(66, 11), ("Play"), 0x1F);
            outputVideo.WriteString(new Point(62, 12), (" R "), 0x30);
            outputVideo.WriteString(new Point(66, 12), ("Restore game"), 0x1E);
            outputVideo.WriteString(new Point(62, 13), (" Q "), 0x70);
            outputVideo.WriteString(new Point(66, 13), ("Quit"), 0x1E);
            outputVideo.WriteString(new Point(62, 16), (" A "), 0x30);
            outputVideo.WriteString(new Point(66, 16), ("About ZZT!"), 0x1F);
            outputVideo.WriteString(new Point(62, 17), (" H "), 0x70);
            outputVideo.WriteString(new Point(66, 17), ("High Scores"), 0x1E);
            outputVideo.WriteString(new Point(62, 21), (" S "), 0x70);
            outputVideo.WriteString(new Point(66, 21), ("Game speed:"), 0x1E);
            outputVideo.WriteString(new Point(66, 23), ("F....:....S"), 0x1E);
        }

        public override void Scroll(ByteString title, ByteString data)
        {
        }

        public override void TextInput()
        {
        }

        public override void UpdateBoard(Location location)
        {
        }

        public override void UpdateHudEditor()
        {
        }

        public override void UpdateHudGame()
        {
            Board board = game.CurrentBoard;
            Parameters parameters = game.Parameters;
            State state = game.State;

            if (board.TimeLimit > 0)
                outputVideo.WriteString(new Point(67, 6), ("Time:" + (board.TimeLimit - info.TimePassed).ToString() + " "), 0x1E);
            else
                RedrawHudLine(6);

            outputVideo.WriteString(new Point(72, 7), (info.Health.ToString() + " "), 0x1E);
            outputVideo.WriteString(new Point(72, 8), (info.Ammo.ToString() + " "), 0x1E);
            outputVideo.WriteString(new Point(72, 9), (info.Torches.ToString() + " "), 0x1E);
            outputVideo.WriteString(new Point(72, 10), (info.Gems.ToString() + " "), 0x1E);
            outputVideo.WriteString(new Point(72, 11), (info.Score.ToString() + " "), 0x1E);

            for (int i = 0; i < parameters.KeyCount; i++)
            {
                AnsiChar keyChar;
                if (info.Keys[i] != 0)
                    keyChar = new AnsiChar(0x0C, (byte)(0x19 + i));
                else
                    keyChar = new AnsiChar(0x20, 0x1E);
                outputVideo.SetChar(new Point(72 + i, 12), keyChar);
            }
            outputVideo.WriteString(new Point(69, 15), state.Quiet ? "Quiet" : "Noisy", 0x1F);

            if (state.Paused)
                outputVideo.WriteString(new Point(64, 5), "Pausing...", 0x1F);
        }

        public override void UpdateHudTitle()
        {
            State state = game.State;

            outputVideo.WriteString(new Point(69, 8), info.Name, 0x1F);
            outputVideo.SetChar(new Point(67 + state.Speed, 22), new AnsiChar(0x1F, 0x1F));
        }

        public override void YesNo()
        {
        }
    }
}
